home *** CD-ROM | disk | FTP | other *** search
/ Aminet 41 / Aminet 41 (2001)(Schatztruhe)[!][Feb 2001].iso / Aminet / dev / c / libmpeg_src.lha / gdith.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-25  |  8.6 KB  |  314 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22.  
  23. #include <config.h>
  24. #include "dither.h"
  25. #include "globals.h"        /* for global variable ditherType */
  26.  
  27. #define NUM_COLORS 256        /* number of entries in colormap */
  28.                 /* for gray-scale dithering */
  29.  
  30. /* Range values for lum, cr, cb. */
  31. int LUM_RANGE = 8;
  32. int CR_RANGE = 4;
  33. int CB_RANGE = 4;
  34.  
  35. /* Array that remaps color numbers to actual pixel values used by X server. */
  36.  
  37. unsigned char pixel[256];
  38.  
  39. /* Arrays holding quantized value ranged for lum, cr, and cb. */
  40.  
  41. int *lum_values;
  42. int *cr_values;
  43. int *cb_values;
  44.  
  45.  
  46. /*
  47.  *--------------------------------------------------------------
  48.  *
  49.  * InitColor --
  50.  *
  51.  *    Initialized lum, cr, and cb quantized range value arrays.
  52.  *
  53.  * Results: 
  54.  *      None.
  55.  *
  56.  * Side effects:
  57.  *      None.
  58.  *
  59.  *--------------------------------------------------------------
  60.  */
  61.  
  62. static void
  63. InitColor()
  64. {
  65.   int i;
  66.  
  67.   for (i=0; i<LUM_RANGE; i++) {
  68.     lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
  69.   }
  70.  
  71.   for (i=0; i<CR_RANGE; i++) {
  72.     cr_values[i] = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
  73.   }
  74.  
  75.   for (i=0; i<CB_RANGE; i++) {
  76.     cb_values[i] = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
  77.   }
  78.  
  79. }
  80.  
  81.  
  82. /*
  83.  *--------------------------------------------------------------
  84.  *
  85.  * ConvertColor --
  86.  *
  87.  *    Given a l, cr, cb tuple, converts it to r,g,b.
  88.  *
  89.  * Results:
  90.  *    r,g,b values returned in pointers passed as parameters.
  91.  *
  92.  * Side effects:
  93.  *      None.
  94.  *
  95.  *--------------------------------------------------------------
  96.  */
  97.  
  98. static void
  99. ConvertColor(l, cr, cb, r, g, b)
  100.      unsigned char l, cr, cb;
  101.      unsigned char *r, *g, *b;
  102. {
  103.   double fl, fcr, fcb, fr, fg, fb;
  104.  
  105.   fl = (double) l;
  106.   fcr =  ((double) cr) - 128.0;
  107.   fcb =  ((double) cb) - 128.0;
  108.  
  109.  
  110.   fr = fl + (1.40200 * fcb);
  111.   fg = fl - (0.71414 * fcb) - (0.34414 * fcr);
  112.   fb = fl + (1.77200 * fcr);
  113.  
  114.   if (fr < 0.0) fr = 0.0;
  115.   else if (fr > 255.0) fr = 255.0;
  116.  
  117.   if (fg < 0.0) fg = 0.0;
  118.   else if (fg > 255.0) fg = 255.0;
  119.  
  120.   if (fb < 0.0) fb = 0.0;
  121.   else if (fb > 255.0) fb = 255.0;
  122.  
  123.   *r = (unsigned char) fr;
  124.   *g = (unsigned char) fg;
  125.   *b = (unsigned char) fb;
  126.  
  127. }
  128.  
  129.  
  130.  
  131. /* ----------------------------- MNI Header -----------------------------------
  132. @NAME       : InitColormap
  133. @INPUT      : (none)
  134. @OUTPUT     : *NumColors - number of entries in the newly-created colormap
  135.               *Map - an array of colourmap entries; each one contains a 
  136.                      red, green, and blue byte-values (0 .. 255).  
  137.              *Map[i] gives the colour to display a pixel value i.
  138. @RETURNS    : (none)
  139. @DESCRIPTION: Creates a colour map used for most dithering methods 
  140.               (everything except full-colour, gray, and monochrome).
  141.           The colour map itself is pretty self-explanatory -- a 
  142.           pixel with value i is to be displayed using the red, green
  143.           and blue values in *Map[i] after InitColormap() is done.
  144. @METHOD     : 
  145. @GLOBALS    : 
  146. @CALLS      : 
  147. @CREATED    : 95/3/4, Greg Ward: based on InitColorDisplay(), from gdith.c
  148.                                  in the original Berkeley player
  149. @MODIFIED   : 
  150. ---------------------------------------------------------------------------- */
  151. #if (ENABLE_DITHER)
  152. static void
  153. InitColormap (int *NumColors, ColormapEntry **Map)
  154. {
  155.    int i, lum_num, cr_num, cb_num;
  156.  
  157.    *NumColors =  LUM_RANGE*CB_RANGE*CR_RANGE;
  158.    if (ditherType == NO_DITHER) return;
  159.    *Map = (ColormapEntry *) malloc (*NumColors * sizeof (ColormapEntry));
  160.  
  161.    for (i = 0; i < *NumColors; i++) 
  162.    {
  163.       lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
  164.       cr_num = (i / CB_RANGE)%CR_RANGE;
  165.       cb_num = i % CB_RANGE;
  166.       
  167.       ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num], 
  168.            &(*Map)[i].red, &(*Map)[i].green, &(*Map)[i].blue);
  169.       pixel[i] = i;
  170.    }
  171. }
  172. #endif
  173.  
  174.  
  175. /* ----------------------------- MNI Header -----------------------------------
  176. @NAME       : InitGrayColormap
  177. @INPUT      : (none)
  178. @OUTPUT     : *NumColors - number of entries in the newly-created colormap
  179.               *Map - an array of colourmap entries
  180. @RETURNS    : (none)
  181. @DESCRIPTION: Creates a colour map used for gray-scale dithering, i.e.
  182.               the red/green/blue values are the same for any given
  183.           pixel value.
  184. @METHOD     : 
  185. @GLOBALS    : 
  186. @CALLS      : 
  187. @CREATED    : 95/3/4, Greg Ward: based on InitGrayDisplay(), from gdith.c
  188.                                  in the original Berkeley player
  189. @MODIFIED   : 
  190. ---------------------------------------------------------------------------- */
  191. #if (ENABLE_DITHER)
  192. static void
  193. InitGrayColormap (int *NumColors, ColormapEntry **Map)
  194. {
  195.    int  i;
  196.    
  197.    *NumColors =  NUM_COLORS;
  198.    if (ditherType == NO_DITHER) return;
  199.    *Map = (ColormapEntry *) malloc (*NumColors * sizeof (ColormapEntry));
  200.  
  201.    for (i = 0; i < *NumColors; i++) 
  202.    {
  203.       (*Map)[i].red = (*Map)[i].green = (*Map)[i].blue = i;
  204.       pixel[i] = i;
  205.    }
  206. }
  207. #endif
  208.  
  209.  
  210. /* ----------------------------- MNI Header -----------------------------------
  211. @NAME       : InitDither
  212. @INPUT      : Image - pointer to the image descriptor for the current MPEG
  213. @OUTPUT     : Image->ColormapSize, Image->Colormap - the colour map for
  214.               this movie, as initialized by either InitColormap or
  215.           InitGrayColormap (unless the current dithering scheme
  216.           is full colour, in which case there is no colour map)
  217. @RETURNS    : (none)
  218. @DESCRIPTION: Does all initialization particular to the type of dithering
  219.               being used.  Basically, sets up the internal data structures
  220.           needed by the dithering code, and then sets up a colour map
  221.           needed to display the pixels output by the ditherers.
  222. @METHOD     : 
  223. @GLOBALS    : 
  224. @CALLS      : InitColor     (for most dithering methods)
  225.               InitColormap  (for most dithering methods)
  226.           InitGrayColormap (for gray-scale dithering)
  227.               Init(..)Dither  (.. = the current dithering method)
  228. @CREATED    : 95/3/3, Greg Ward: taken mostly from main() in the original 
  229.                                  Berkeley player
  230. @MODIFIED   : 
  231. ---------------------------------------------------------------------------- */
  232. void
  233. InitDither (ImageDesc *Image)
  234. {
  235.    switch (ditherType)
  236.    {
  237. #if (ENABLE_DITHER)
  238.       case HYBRID_DITHER:
  239.      InitColor ();
  240.      InitHybridDither ();
  241.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  242.      break;
  243.  
  244.       case HYBRID2_DITHER:
  245.      InitColor ();
  246.      InitHybridErrorDither ();
  247.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  248.      break;
  249.  
  250.       case FS4_DITHER:
  251.      InitColor ();
  252.      InitFS4Dither ();
  253.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  254.      break;
  255.  
  256.       case FS2_DITHER:
  257.      InitColor ();
  258.      InitFS2Dither ();
  259.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  260.      break;
  261.  
  262.       case FS2FAST_DITHER:
  263.      InitColor ();
  264.      InitFS2FastDither ();
  265.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  266.      break;
  267.  
  268.       case Twox2_DITHER:
  269.      InitColor ();
  270.      Init2x2Dither ();
  271.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  272.      PostInit2x2Dither ();
  273.      break;
  274.  
  275.       case GRAY_DITHER:
  276.      InitGrayColormap (&Image->ColormapSize, &Image->Colormap);
  277.      break;
  278. #endif
  279.       case FULL_COLOR_DITHER:
  280.      InitColorDither ();
  281.      Image->ColormapSize = -1;
  282.      Image->Colormap = NULL;
  283.      break;
  284.  
  285. #if (ENABLE_DITHER)
  286.       case NO_DITHER:
  287.      break;
  288.  
  289.       case ORDERED_DITHER:
  290.      InitColor ();
  291.      InitOrderedDither ();
  292.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  293.      break;
  294.  
  295.       case MONO_DITHER:
  296.       case MONO_THRESHOLD:
  297.      break;
  298.  
  299.       case ORDERED2_DITHER:
  300.      InitColor ();
  301.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  302.      InitOrdered2Dither ();
  303.      break;
  304.  
  305.       case MBORDERED_DITHER:
  306.      InitColor ();
  307.      InitColormap (&Image->ColormapSize, &Image->Colormap);
  308.      InitMBOrderedDither ();
  309.      break;
  310. #endif
  311.    }
  312. }   
  313.  
  314.